/**
 * Created by Administrator on 2018/6/26.
 */
var tenderGrid = {};
$CertusFormAjax.ajax("phaseBidExamine", {
    proId: proId
}, function (res) {
    if (res.data != null) {
        $$scope.allBidInfo = res.data;

        // $http({
        //     // url: "proinfo/edit/edit-compact.html",
        //     url: "proinfo/edit/edit-bid-sub.html",
        //     method: 'GET',
        //     data: {},
        //     dataType: "application/json"
        // }).then(function (bb) {
        //     $$scope.bidsubpage = bb.data;
        // }, function (b) {
        // })
        // initGrid();
        // extracted1();
        // for(var index in $$scope.allBidInfo){
        //     $$http({
        //         // url: "proinfo/edit/edit-compact.html",
        //         url: "proinfo/edit/edit-bid-sub.html",
        //         method: 'GET',
        //         data: {},
        //         dataType: "application/json"
        //     }).then(function (a) {
        //
        //         $$scope.ss = a.data;
        //         $$scope.now = $$scope.allBidInfo;
        //         // $CertusFormAjax = $certusUtils;
        //     }, function (b) {
        //     })
        // }


        if ($$scope.allBidInfo.bid) {

        }
        //console.log(res.data.length);
        if (res.data.length == 0) {
            $$scope.notHasBid = true;
        } else {
            $$scope.notHasBid = false;
        }
    }
});


function initGrid() {
    for (var index in $$scope.allBidInfo) {
        var tenderOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '投标文件列表',
            datas: $$scope.allBidInfo[index].file,
            columns: tenderColumns,
            gridContainer: 'proContainer-' + index,
            toolbarContainer: 'proToolBarContainer-' + index,
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        tenderGrid[index] = $.fn.dlshouwen.grid.init(tenderOption);
        tenderGrid[index].load();
    }

}


function extracted1() {

    var data = $$scope.allBidInfo;

    angular.forEach(data, function (obj, index) {
        $(function () {


            tenderGrid[index].exhibitDatas = obj.file;
            tenderGrid[index].refresh(false);

            $CertusJsonAjax.ajax("queryBidProcessTrace", {
                "processInstanceId": $$scope.allBidInfo[index].phaseInfo[0].processInstanceId,
                "stepInstanceId": $$scope.allBidInfo[index].phaseInfo[0].stepInstanceId
            }, function (res) {
                $$scope["processs" + index] = res.data;
            })
        })

        // stepBar.init("stepBar-1" + "-" + index, {
        //     step: obj.phaseNumber,
        //     change: false,
        //     animation: true
        // });
    })
}
// $$scope.timer2 = $$interval(function () {
//     if ($("#stepBar-1-0").html() && $$scope.allBidInfo) {
//         // extracted1();
//         if ($("#stepBar-1-0").html()) {
//             extracted1();
//         } else {
//             $$scope.timer2 = $$timeout(function () {
//                 if ($("#stepBar-1-0").html()) {
//                     extracted1();
//                 }else{
//                     $$scope.timer3 = $$timeout(function () {
//                         if ($("#stepBar-1-0").html()) {
//                             extracted1();
//                         }else{
//                             $$scope.timer4 = $$timeout(function () {
//                                 if ($("#stepBar-1-0").html()) {
//                                     extracted1();
//                                 }else{
//                                     layer.msg("重载 重启页面");
//                                     window.location.reload()
//                                 }
//                             }, 50)
//                         }
//                     }, 50)
//                 }
//             }, 50)
//         }
//     }
// }, 500)

$$scope.$on('to-parent', function (event, a, b) {
    // console.log('BroCtrl', data);

    //平级得不到值

    // $CertusFormAjax = $certusUtils;

// 投标文件

});

$$scope.initBidFile = function(a,b,c){

    var tenderColumns1 = [{
        id: 'fileCategories',
        title: '文件类目',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'fileClass',
        title: '文件类别',
        width: 120,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'fileOriginName',
        title: '文件名',
        // width: 150,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'opinionMsg',
        title: '文件说明',
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'version',
        title: '文件版本',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'userName',
        title: '上传人员',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'createTime',
        title: '上传时间',
        width: 100,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'operation',
        title: '操作',
        type: 'string',
        width: 150,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {

            var met = {
                'XMTBXXC': "update(\'" + record.processInstanceId + "\',\'" + record.stepInstanceId + "\',\'" + record.pageUrl + "\');",
                'XMTBXXR': "down(\'" + record.processInstanceId + "\',\'" + record.stepInstanceId + "\');"
            }
            content = $CertusFormAjax.initOperateAutho($$scope.leftMenu, ['XMTBXXC%upload', 'XMTBXXR%download'], met, tenderColumns);
            return content;
        }
    }];

    var tenderOption = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '投标文件列表',
        datas: b,
        columns: tenderColumns1,
        gridContainer: 'proContainer' + a,
        toolbarContainer: 'proToolBarContainer' + a,
        tools: '',
        pageSize: 10,
        pageSizeLimit: [10, 15, 20]
    };
    var tenderGrid = $.fn.dlshouwen.grid.init(tenderOption);
    tenderGrid.load();
    $(c.target).remove();
}

// 投标文件
var tenderColumns = [{
    id: 'fileCategories',
    title: '文件类目',
    width: 80,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'fileClass',
    title: '文件类别',
    width: 120,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'fileOriginName',
    title: '文件名',
    // width: 150,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'opinionMsg',
    title: '文件说明',
    type: 'string',
    columnClass: 'text-left',
    hideType: 'lg|md|sm|xs'
}, {
    id: 'version',
    title: '文件版本',
    width: 80,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'userName',
    title: '上传人员',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'createTime',
    title: '上传时间',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'operation',
    title: '操作',
    type: 'string',
    width: 150,
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var met = {
            'XMTBXXC': "update(\'" + record.processInstanceId + "\',\'" + record.stepInstanceId + "\',\'" + record.pageUrl + "\');",
            'XMTBXXR': "down(\'" + record.processInstanceId + "\',\'" + record.stepInstanceId + "\');"
        }
        content = $CertusFormAjax.initOperateAutho($$scope.leftMenu, ['XMTBXXC%upload', 'XMTBXXR%download'], met, tenderColumns);
        return content;
    }
}];

$$scope.getsssbbb = function (bb) {
    if (bb == "starting") {
        return "正在进行中";
    } else if (bb == "denied") {
        return "否决";
    } else if (bb == "sentback") {
        return "被退";
    } else if (bb == "others") {
        return "关闭";
    } else if (bb == "completed") {
        return "完成";
    } else if (bb == "terminated") {
        return "终结";
    } else if (bb == "cancelled") {
        return "通过";
    }
    return "待办";
}

function update(processInstanceId, stepInstanceId, pageUrl) {
    $CertusJsonAjax.setWorkflowInfo(processInstanceId, stepInstanceId);
    ;
    $CertusJsonAjax.ajax("verifyStatus", {
        "processInstanceId": $CertusJsonAjax.getProInstanceId(),
        "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
    }, function (res) {
        if (res.data == "success") {
            $$http({
                url: "templates/workflow/schedule/TBLC/" + pageUrl + "-ALTER.html",
                method: 'GET',
                data: {},
                dataType: "application/json"
            }).then(function (a) {
                $$scope.htmlxx = a.data
            }, function (b) {
            })
        } else if (res.data == "complete") {
            layer.msg("投标已完成，不能继续更新");
        } else {
            layer.msg("当前，文件不能更新");
        }
    })
}


function down(processInstanceId, stepInstanceId) {
    $CertusJsonAjax.ajax("processFile", {
        processInstanceId: processInstanceId,
        stepInstanceId: stepInstanceId
    }, function (data) {
        $$scope.modalContent = data.data;
        $$scope.ok = function () {
            $modalInstance.close(); // 点击确认后关闭弹窗
        };
        $$scope.cancel = function () {
            $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
        }
        var modalContent = $$scope.modalContent;

        $fileListService.openConfirmWindow("提示", modalContent).then(function (res) {
        });
    })
}

function downLoad(id) {
    var fileID = $(id).parent().parent().find(".fileDownloadPath").val();
    $CertusFormAjax.download("downloadFile", "fileId=" + fileID);
}